/**
 * 对图片转base64
 *
 * @param file
 * @return {Promise<any>} base64
 */
export const file2Base64 = (file: any) => {
    return new Promise(resolve => {
        const reader = new FileReader()
        reader.onload = (e => {
            let base64 = e?.target?.result
            resolve(base64)
        })
        reader.readAsDataURL(file)
    })
}

//将base64转换为文件
export const dataURLtoFile = (base64Str: any, filename: any) => {
    //默认文件名
    filename = filename || new Date().getTime() + ".jpg";
    let arr = base64Str.split(','),
        mime = arr[0].match(/:(.*?);/)[1],
        bstr = atob(arr[1]),
        n = bstr.length,
        u8arr = new Uint8Array(n);
    while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
    }
    return new File([u8arr], filename, {type: mime});
}
